<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;

use app\common\logic\Points as CommonPoints;

/**
 * 积分接口逻辑
 */
class Points extends ApiBase
{
    
    public static $commonPointsLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonPointsLogic) && static::$commonPointsLogic = new CommonPoints();
    }
    
    /**
     * 每日签到
     */
    public function dailyCheckin($data = [])
    {
        // JWT解析用户信息
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id']; // 普通用户使用user_id
            
            // 检测并更新VIP状态
            $userLogic = new \app\common\logic\User();
            $userLogic->checkAndUpdateVipStatus($userData['user_id']);
        }
        
        // 调用common层
        $result = static::$commonPointsLogic->dailyCheckin($data);
        
        // 处理common层返回的[状态码, 消息, 数据]格式
        if (is_array($result) && isset($result[0])) {
            if ($result[0] == RESULT_SUCCESS) {
                return isset($result[2]) ? $result[2] : '操作成功';
            } else {
                return $this->returnError($result[1] ?? '操作失败');
            }
        }
        
        return $result;
    }
    
    /**
     * 获取签到状态
     */
    public function getCheckinStatus($data = [])
    {
        // JWT解析用户信息
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id']; // 普通用户使用user_id
            
            // 检测并更新VIP状态
            $userLogic = new \app\common\logic\User();
            $userLogic->checkAndUpdateVipStatus($userData['user_id']);
        }
        
        // 调用common层
        $result = static::$commonPointsLogic->getCheckinStatus($data);
        
        // 处理common层返回的[状态码, 消息, 数据]格式
        if (is_array($result) && isset($result[0])) {
            if ($result[0] == RESULT_SUCCESS) {
                return isset($result[2]) ? $result[2] : [];
            } else {
                return $this->returnError($result[1] ?? '获取失败');
            }
        }
        
        return $result;
    }
    
    /**
     * 获取积分商城
     */
    public function getPointsMall($data = [])
    {
        // 参数处理
        $where = [];
        if (!empty($data['category'])) {
            $where['category'] = $data['category'];
        }
        
        // 分页参数
        $paginate = [
            'list_rows' => $data['limit'] ?? 20,
            'page' => $data['page'] ?? 1
        ];
        
        // 调用common层
        $result = static::$commonPointsLogic->getPointsMall($where, true, 'sort desc,id desc', $paginate);
        
        // 处理common层返回的[状态码, 消息, 数据]格式
        if (is_array($result) && isset($result[0])) {
            if ($result[0] == RESULT_SUCCESS) {
                return isset($result[2]) ? $result[2] : [];
            } else {
                return $this->returnError($result[1] ?? '获取失败');
            }
        }
        
        return $result;
    }
    
    /**
     * 兑换商品
     */
    public function exchangeProduct($data = [])
    {
        // JWT解析用户信息
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id']; // 普通用户使用user_id
            
            // 检测并更新VIP状态
            $userLogic = new \app\common\logic\User();
            $userLogic->checkAndUpdateVipStatus($userData['user_id']);
        }
        
        // 调用common层
        $result = static::$commonPointsLogic->exchangeProduct($data);
        
        // 处理common层返回的[状态码, 消息, 数据]格式
        if (is_array($result) && isset($result[0])) {
            if ($result[0] == RESULT_SUCCESS) {
                return isset($result[2]) ? $result[2] : '兑换成功';
            } else {
                return $this->returnError($result[1] ?? '兑换失败');
            }
        }
        
        return $result;
    }
    
    /**
     * 获取积分记录
     * @author 王勇程 <laughskydragon@qq.com>
     * @param array $data
     * @return array
     */
    public function getPointsRecords($data = [])
    {
        // 1. JWT解析获取user_id
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id'];
        }

        // 2. 调用getWhere方法
        $where = static::$commonPointsLogic->getWhere($data);

        // 3. 调用common层业务逻辑
        $result = static::$commonPointsLogic->getPointsRecords($where);

        return $result;
    }
    
    /**
     * 获取用户积分信息
     */
    public function getUserPointsInfo($data = [])
    {
        // JWT解析用户信息
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id']; // 普通用户使用user_id
        }
        
        // 调用common层
        $result = static::$commonPointsLogic->getUserPointsInfo($data);
        
        // 处理common层返回的[状态码, 消息, 数据]格式
        if (is_array($result) && isset($result[0])) {
            if ($result[0] == RESULT_SUCCESS) {
                return isset($result[2]) ? $result[2] : [];
            } else {
                return $this->returnError($result[1] ?? '获取失败');
            }
        }
        
        return $result;
    }
    
    /**
     * 获取商品详情
     */
    public function getProductDetail($data = [])
    {
        // 调用common层
        return static::$commonPointsLogic->getProductDetail($data);
    }
    
    /**
     * 获取兑换记录
     */
    public function getExchangeRecords($data = [])
    {
        // JWT解析用户信息
        $token = $data['user_token'] ?? '';
        if (!empty($token)) {
            $userData = get_user_by_token($token);
            $data['user_id'] = $userData['user_id']; // 普通用户使用user_id
        }
        
        // 分页参数
        $paginate = [
            'list_rows' => $data['limit'] ?? 20,
            'page' => $data['page'] ?? 1
        ];
        
        // 调用common层
        return static::$commonPointsLogic->getExchangeRecords($data, $paginate);
    }
}